/**
 * 
 */
package fr.lelouet.ga.examples.travelingSalesman;

import fr.lelouet.ga.Generation;
import fr.lelouet.ga.Generator;
import fr.lelouet.ga.utils.SetUtils;

/**
 * generates a given number of Travels, which are correct on the given WorldMap
 * 
 * @author guigolum
 */
public class TravelGenerator implements Generator<Travel> {

    /** number of travels we want to generate */
    protected int number;

    /** the cost of the moves */
    protected WorldMap world;

    /**
     * generate a Generation of given {@link #number} of {@link Travel}, with at
     * least a simple hamiltonian travel from the internal {@link #world}, and
     * some mutation of this one.<br />
     * No Travel is specified to be of finite length !
     */
    @Override
    public Generation<Travel> generate() {
	final Generation<Travel> ret = new Generation<Travel>();
	ret.add(world.simpleHamiltonien());
	while (ret.size() < number) {
	    Travel origine = SetUtils.getAny(ret);
	    if (origine != null) {
		ret.add(TravelMutator.muteOneTown(origine));
	    }
	}
	return ret;
    }

    /**
     * 
     * @param number
     *            the number of travel to generate
     * @param world
     *            the map of distances to use.
     */
    public TravelGenerator(int number, WorldMap world) {
	this.number = number;
	this.world = world;
    }

}
